<script setup>
defineProps({
  query: String,
  engine: Object,
  engines: Array
})

defineEmits(['update:query', 'update:engine', 'search'])
</script>

<template>
  <div class="search-bar">
    <div class="search-container">
      <div class="search-input-wrapper">
        <input
          :value="query"
          @input="$emit('update:query', $event.target.value)"
          @keyup.enter="$emit('search')"
          class="search-input"
          type="text"
          placeholder="搜索网页..."
          autofocus
        />
        <button @click="$emit('search')" class="search-button">
          <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor">
            <circle cx="11" cy="11" r="8"></circle>
            <path d="m21 21-4.3-4.3"></path>
          </svg>
        </button>
      </div>
      
      <select
        :value="engine"
        @change="$emit('update:engine', $event.target.value)"
        class="search-engine-select"
      >
        <option v-for="engine in engines" :key="engine.name" :value="engine">
          {{ engine.name }}
        </option>
      </select>
    </div>
    
    <div class="search-tips">
      <span>提示: 按 Ctrl+K 快速聚焦搜索框</span>
    </div>
  </div>
</template>

<style scoped>
.search-bar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  width: 100%;
}

.search-container {
  display: flex;
  gap: 1rem;
  align-items: center;
  width: 100%;
  max-width: 600px;
}

.search-input-wrapper {
  position: relative;
  flex: 1;
  min-width: 0; /* 防止flex item溢出 */
}

.search-input {
  width: 100%;
  padding: 1rem 3rem 1rem 1.5rem;
  font-size: 1.1rem;
  border: none;
  border-radius: 50px;
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  outline: none;
  -webkit-appearance: none; /* 移除iOS默认样式 */
}

.search-input:focus {
  background: white;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.search-button {
  position: absolute;
  right: 0.5rem;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  color: #666;
  padding: 0.5rem;
  border-radius: 50%;
  transition: all 0.3s ease;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.search-button:hover {
  background: rgba(0, 0, 0, 0.1);
  color: #333;
}

.search-engine-select {
  padding: 1rem;
  border: none;
  border-radius: 50px;
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  font-size: 1rem;
  outline: none;
  min-width: 120px;
  cursor: pointer;
  -webkit-appearance: none; /* 移除iOS默认样式 */
  appearance: none;
}

.search-tips {
  color: rgba(255, 255, 255, 0.8);
  font-size: 0.9rem;
  margin-top: 0.5rem;
  text-align: center;
}

/* 平板设备 (768px - 1024px) */
@media (max-width: 1024px) and (min-width: 769px) {
  .search-container {
    max-width: 500px;
  }
  
  .search-input {
    padding: 0.9rem 2.8rem 0.9rem 1.3rem;
    font-size: 1rem;
  }
  
  .search-engine-select {
    padding: 0.9rem;
    font-size: 0.9rem;
    min-width: 110px;
  }
}

/* 移动设备 (768px 以下) */
@media (max-width: 768px) {
  .search-bar {
    gap: 0.8rem;
  }
  
  .search-container {
    flex-direction: column;
    gap: 0.8rem;
    max-width: 100%;
  }
  
  .search-input-wrapper {
    width: 100%;
  }
  
  .search-input {
    padding: 0.8rem 2.5rem 0.8rem 1.2rem;
    font-size: 1rem;
    border-radius: 25px;
  }
  
  .search-button {
    width: 35px;
    height: 35px;
    right: 0.4rem;
  }
  
  .search-engine-select {
    width: 100%;
    max-width: 200px;
    padding: 0.8rem;
    font-size: 0.9rem;
    border-radius: 25px;
  }
  
  .search-tips {
    font-size: 0.8rem;
    margin-top: 0.3rem;
  }
}

/* 小屏手机 (480px 以下) */
@media (max-width: 480px) {
  .search-bar {
    gap: 0.6rem;
  }
  
  .search-container {
    gap: 0.6rem;
  }
  
  .search-input {
    padding: 0.7rem 2.2rem 0.7rem 1rem;
    font-size: 0.9rem;
  }
  
  .search-button {
    width: 30px;
    height: 30px;
    right: 0.3rem;
  }
  
  .search-engine-select {
    max-width: 180px;
    padding: 0.7rem;
    font-size: 0.85rem;
  }
  
  .search-tips {
    font-size: 0.75rem;
    display: none; /* 在小屏手机上隐藏提示 */
  }
}

/* 横屏手机优化 */
@media (max-height: 500px) and (orientation: landscape) {
  .search-bar {
    gap: 0.5rem;
  }
  
  .search-container {
    flex-direction: row;
    max-width: 90%;
  }
  
  .search-tips {
    display: none;
  }
}

/* 触摸设备优化 */
@media (hover: none) and (pointer: coarse) {
  .search-button:hover {
    background: none;
  }
  
  .search-engine-select {
    font-size: 16px; /* 防止iOS缩放 */
  }
}
</style>
